Add two integers (>0) without using the ‘+’ operator¶
Add two positive integers without using the ‘+’ operator.
Note: Use bit wise operations to add two numbers.
def add_without_plus_operator(a, b):
while b != 0:
data = a & b
a = a ^ b
b = data << 1
return a
print(add_without_plus_operator(2, 10))
print(add_without_plus_operator(-20, 10))
print(add_without_plus_operator(-10, -20))
Output:
12
-10
-30